package 贪心;

/**
 * @ClassName _135分发糖果
 * @Description TODO
 * @Author 昝亚杰
 * @Date 2021/12/21 20:01
 * Version 1.0
 **/
public class _135分发糖果 {
    public int candy(int[] ratings) {
        int[] candyVal = new int[ratings.length];
        for (int i = 0; i < ratings.length; i++){
            candyVal[i] = 1;
        }
        for (int i = 1; i < ratings.length; i++){
            if(ratings[i] > ratings[i - 1]){
                candyVal[i] = candyVal[i - 1] + 1;
            }
        }
        for (int i = ratings.length - 1; i > 0; i--){
            if(ratings[i - 1] > ratings[i]){
                candyVal[i - 1] = Math.max(candyVal[i] + 1,candyVal[i - 1]);
            }
        }
        int count = 0;
        for(int i = 0; i < candyVal.length; i++){
            count += candyVal[i];
        }
        return count;
    }
}
